package com.lc.advertising.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.http.listener.OnHttpListener;
import com.hjq.toast.ToastUtils;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.glide.GlideApp;
import com.lc.advertising.http.request.CityApi;
import com.lc.advertising.http.request.RenzhengApi;
import com.lc.advertising.http.request.RenzhengSaveApi;
import com.lc.advertising.http.request.UpdateImageApi;
import com.lc.advertising.http.response.CityBean;
import com.lc.advertising.http.response.RegisterBean;
import com.lc.advertising.http.response.RenzhengBean;
import com.lc.advertising.http.response.UpdateImageBean;
import com.lc.advertising.other.IntentKey;
import com.lc.advertising.util.CourseUtils;
import com.lc.advertising.util.GlideEngine;
import com.lc.advertising.util.SPUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class AttestationActivity extends AppActivity {
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_carnum)
    EditText etCarnum;
    @BindView(R.id.et_cardata)
    EditText etCardata;
    @BindView(R.id.img_papers_front)
    ImageView imgPapersFront;
    @BindView(R.id.img_papers_back)
    ImageView imgPapersBack;
    @BindView(R.id.check_tongyi)
    CheckBox checkTongyi;
    @BindView(R.id.img_driver)
    ImageView imgDriver;
    @BindView(R.id.img_work)
    ImageView imgWork;
    @BindView(R.id.img_vehicle)
    ImageView imgVehicle;
    @BindView(R.id.tv_driver)
    TextView tvDriver;
    @BindView(R.id.tv_work)
    TextView tvWork;
    @BindView(R.id.tv_city)
    TextView tvCity;

    @BindView(R.id.tv_vehicle)
    TextView tvVehicle;
    private RxPermissions rxPermissions;
    private String type;
    // 省集合
    private ArrayList<String> options1Items = new ArrayList<>();
    //市集合
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<String> strings;
    private String tx;
    private String zheng;
    private String fan;
    private String driver;
    private String work;
    private String vehicle;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_attestation;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        renzheng();
        rxPermissions = new RxPermissions(this);
    }



    @OnClick({R.id.rl_back, R.id.tv_city, R.id.img_papers_front, R.id.img_papers_back, R.id.tv_driver, R.id.tv_work, R.id.tv_vehicle, R.id.btn_login_commit, R.id.img_driver, R.id.img_work, R.id.img_vehicle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.tv_city:
               city();
//
                break;
            case R.id.img_papers_front:
                type = "front";
                carem(type);
                break;
            case R.id.img_papers_back:
                type = "back";
                carem(type);
                break;
            case R.id.tv_driver:
                type = "driver";
                carem(type);
                break;
            case R.id.tv_work:
                type = "work";
                carem(type);
                break;
            case R.id.tv_vehicle:
                type = "vehicle";
                carem(type);
                break;
            case R.id.img_driver:
                type = "driver";
                carem(type);
                break;
            case R.id.img_work:
                type = "work";
                carem(type);
                break;
            case R.id.img_vehicle:
                type = "vehicle";
                carem(type);
                break;
            case R.id.btn_login_commit:
                if (checkTongyi.isChecked()){
                    renzheng_save();
                }else {
                    toast("请同意用户协议");
                }
                break;
        }
    }

    private void renzheng_save() {
        EasyHttp.post(this)
                .api(new RenzhengSaveApi()
                        .setUid(SPUtil.get(this, IntentKey.UID,""))
                        .setCity(tx)
                        .setCar_number(etCarnum.getText().toString())
                        .setName(etUsername.getText().toString())
                        .setCar_info(etCardata.getText().toString())
                        .setZheng_pic(zheng)
                        .setFan_pic(fan)
                        .setDrive_pic(driver)
                        .setYun_ypic(vehicle)
                        .setGang_pic(work))
                .request(new HttpCallback<RegisterBean>(this) {


                    @Override
                    public void onSucceed(RegisterBean data) {
                        toast(data.getMessage());
                        if (data.getCode()==200){
                            finish();
                        }
                    }


                });
    }
    private void renzheng() {
        EasyHttp.post(this)
                .api(new RenzhengApi()
                        .setUid(SPUtil.get(this,IntentKey.UID,"")))
                .request(new HttpCallback<RenzhengBean>(this) {


                    @Override
                    public void onSucceed(RenzhengBean data) {

                        if (data.getCode()==200){
                            if (data.getList()!=null) {
                                etUsername.setText(data.getList().getName());
                                etCarnum.setText(data.getList().getCar_number());
                                etCarnum.setText(data.getList().getCar_number());
                                etCardata.setText(data.getList().getCar_info());
                                tvCity.setText(data.getList().getCity());
                                if (CourseUtils.isNotNullOrEmpty(data.getList().getYun_ypic())){
                                    GlideApp.with(AttestationActivity.this).load(data.getList().getYun_ypic()).into(imgVehicle);
                                    tvVehicle.setVisibility(View.GONE);
                                    imgVehicle.setVisibility(View.VISIBLE);
                                }
                                if (CourseUtils.isNotNullOrEmpty(data.getList().getGang_pic())){
                                    GlideApp.with(AttestationActivity.this).load(data.getList().getGang_pic()).into(imgWork);
                                    imgWork.setVisibility(View.VISIBLE);
                                    tvWork.setVisibility(View.GONE);
                                }
                                if (CourseUtils.isNotNullOrEmpty(data.getList().getDrive_pic())){
                                    GlideApp.with(AttestationActivity.this).load(data.getList().getDrive_pic()).into(imgDriver);
                                    imgDriver.setVisibility(View.VISIBLE);
                                    tvDriver.setVisibility(View.GONE);
                                }

                                GlideApp.with(AttestationActivity.this).load(data.getList().getFan_pic()).into(imgPapersBack);
                                GlideApp.with(AttestationActivity.this).load(data.getList().getZheng_pic()).into(imgPapersFront);
                            }
                        }
                    }


                });
    }
    private void city() {
        EasyHttp.post(this)
                .api(new CityApi())
                .request(new OnHttpListener<CityBean>() {

                    @Override
                    public void onSucceed(CityBean data) {
//

                        for (int i = 0; i <data.getProv().size() ; i++) {
                            options1Items.add(data.getProv().get(i).getName());
                            strings = new ArrayList<>();
                            for (int j = 0; j <data.getProv().get(i).getList().size() ; j++) {
                                strings.add(data.getProv().get(i).getList().get(j).getName());
                            }
                            options2Items.add(strings);

                        }

                        OptionsPickerView pvOptions = new OptionsPickerBuilder(AttestationActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                        //返回的分别是三个级别的选中位置
                        tx = options1Items.get(options1)+" "+ options2Items.get(options1).get(option2);

                        tvCity.setText(tx);
                    }
                }).build();
                pvOptions.setPicker(options1Items, options2Items);
                pvOptions.show();
                    }

                    @Override
                    public void onFail(Exception e) {

                    }
                });

    }

    private void carem(String tpye) {
        rxPermissions
                .request(Manifest.permission.CAMERA)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            PictureSelector.create(AttestationActivity.this)
                                    .openGallery(PictureMimeType.ofImage())// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                                    .maxSelectNum(1)
                                    .imageEngine(GlideEngine.createGlideEngine())// 外部传入图片加载引擎，必传项
                                    .imageSpanCount(4)// 每行显示个数
                                    .selectionMode(PictureConfig.SINGLE)// 多选 or 单选PictureConfig.MULTIPLE : PictureConfig.SINGLE
                                    .isCamera(true)// 是否显示拍照按钮
                                    .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
//                                    .isEnableCrop(true)
//                                    .withAspectRatio(1, 1)// 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                                    .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
                        } else {
                            ToastUtils.show("请求权限失败,相机不可用");
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
            File headFile = new File(selectList.get(0).getRealPath());
            getCommon_uploads(headFile);


        }
    }

    private void getCommon_uploads(File headFile) {
        EasyHttp.post(this)
                .api(new UpdateImageApi()
                        .setPic(headFile))
                .request(new OnHttpListener<UpdateImageBean>() {

                    @Override
                    public void onSucceed(UpdateImageBean data) {
//
                        switch (type) {
                            case "front":
                                GlideApp.with(AttestationActivity.this).load(data.getImg()).into(imgPapersFront);
                                zheng = data.getImg();
                                break;
                            case "back":
                                fan = data.getImg();
                                GlideApp.with(AttestationActivity.this).load(data.getImg()).into(imgPapersBack);
                                break;
                            case "driver":
                                imgDriver.setVisibility(View.VISIBLE);
                                tvDriver.setVisibility(View.GONE);
                                driver = data.getImg();
                                GlideApp.with(AttestationActivity.this).load(data.getImg()).into(imgDriver);
                                break;
                            case "work":
                                imgWork.setVisibility(View.VISIBLE);
                                tvWork.setVisibility(View.GONE);
                                work = data.getImg();
                                GlideApp.with(AttestationActivity.this).load(data.getImg()).into(imgWork);
                                break;
                            case "vehicle":
                                vehicle = data.getImg();
                                tvVehicle.setVisibility(View.GONE);
                                imgVehicle.setVisibility(View.VISIBLE);
                                GlideApp.with(AttestationActivity.this).load(data.getImg()).into(imgVehicle);
                                break;

                        }

                    }

                    @Override
                    public void onFail(Exception e) {

                    }
                });
    }
}
